<template>
  <div>
    <!-- 添加或修改定时任务对话框 -->
    <el-dialog
      :model-value="open"
      :close-on-click-modal="false"
      :title="title"
      width="800px"
      append-to-body
      :before-close="handleClose"
    >
      <el-form
        ref="editForm"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="jobName">
              <el-input v-model="form.jobName" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务分组" prop="jobGroup">
              <el-select v-model="form.jobGroup" placeholder="请选择">
                <el-option
                  v-for="dict in dicts[DictDirection.任务分组]"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="invokeTarget">
              <template #label>
                <span>
                  调用方法
                  <el-tooltip placement="top">
                    <template #content>
                      <div>
                        Bean调用示例：ryTask.ryParams('ry')
                        <br />Class类调用示例：com.ruoyi.quartz.task.RyTask.ryParams('ry')
                        <br />参数说明：支持字符串，布尔类型，长整型，浮点型，整型
                      </div>
                    </template>
                    <el-icon><question-filled /></el-icon>
                  </el-tooltip>
                </span>
              </template>
              <el-input v-model="form.invokeTarget" placeholder="请输入调用目标字符串" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="cron表达式" prop="cronExpression">
              <el-input v-model="form.cronExpression" placeholder="请输入cron执行表达式">
                <template #append>
                  <el-button type="primary" @click="handleShowCron">
                    生成表达式
                  </el-button>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="执行策略" prop="misfirePolicy">
              <el-radio-group v-model="form.misfirePolicy">
                <el-radio-button
                  v-for="dict in dicts[DictDirection.执行策略]"
                  :key="dict.dictValue"
                  :value="dict.dictValue"
                  :label="dict.dictValue"
                >
                  {{ dict.dictLabel }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否并发" prop="concurrent">
              <el-radio-group v-model="form.concurrent">
                <el-radio-button
                  v-for="dict in dicts[DictDirection.是否并发]"
                  :key="dict.dictValue"
                  :value="dict.dictValue"
                  :label="dict.dictValue"
                >
                  {{ dict.dictLabel }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dicts[DictDirection.任务状态]"
                  :key="dict.dictValue"
                  :value="dict.dictValue"
                  :label="dict.dictValue"
                >
                  {{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, defineExpose, defineEmits, defineProps } from 'vue'
import { ElMessage } from 'element-plus'
import { addJob, updateJob, listJob } from '@/api/monitor/job'
import useDicts, { DictDirection } from '@/store/modules/dicts'

const dicts = useDicts()
dicts.setDicts(DictDirection.任务分组)
dicts.setDicts(DictDirection.任务状态)
dicts.setDicts(DictDirection.执行策略)
dicts.setDicts(DictDirection.是否并发)
const expression = ref('')
const openCron = ref(false)
const props = defineProps({
  open: {
    type: Boolean,
  },
  title: {
    type: String,
  },
})
const 
  form = ref({
    jobId: undefined,
    jobName: undefined,
    jobGroup: undefined,
    invokeTarget: undefined,
    cronExpression: undefined,
    misfirePolicy: '1',
    concurrent: '1',
    status: 1,
  }),
  rules = {
    jobName: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
    invokeTarget: [{ required: true, message: '调用目标字符串不能为空', trigger: 'blur' }],
    cronExpression: [{ required: true, message: 'cron执行表达式不能为空', trigger: 'blur' }],
  },
  editForm = ref(null),
  {
    proxy,
  }:{
    proxy:any
  } = getCurrentInstance()
const emit = defineEmits(['changeList', 'update:open'])
const reset = () => {
  form.value = {
    jobId: undefined,
    jobName: undefined,
    jobGroup: undefined,
    invokeTarget: undefined,
    cronExpression: undefined,
    misfirePolicy: '1',
    concurrent: '1',
    status: 1,
  }
  proxy.$resetForm(editForm.value)
}
/** cron表达式按钮操作 */
const handleShowCron = () => {
  // expression.value = form.value.cronExpression
  // openCron.value = true
}
// 确定新增-修改
const submitForm = () => {
  proxy.$validateForm(editForm.value, () => {
    if (form.value.jobId != undefined) {
      updateJob(form.value).then(res => {
        if (res.code == 200) {
          ElMessage.success('修改成功')
          emit('update:open', false)
          emit('changeList')
        }
      })
    } else {
      addJob(form.value).then(res => {
        if (res.code == 200) {
          ElMessage.success('新增成功')
          reset()
          emit('update:open', false)
          emit('changeList')
        }
      })
    }
  })
}
const handleClose = () => {
  reset()
  emit('update:open', false)
}
const cancel = () => {
  reset()
  emit('update:open', false)
}
const getList = (id) => {
  listJob(id)
  .then((res) => {
    if (res.code === 200) {
      form.value = res.data[0]
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((err) => {
    console.log(err)
    ElMessage.error(err)
  })
}
defineExpose({
  getList,
})
</script>
l
